﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;
using DAL;
using DAL.IRepository;

namespace WindowsApp.QLTaiKhoan
{
    public partial class frmQLTaiKhoan : DevComponents.DotNetBar.Office2007Form
    {
        private IGeneralRepository<TaiKhoan> _repository;
        private TaiKhoanManager _manager;
        private TaiKhoan _tk;
        private bool _selectAll = false;
        public frmQLTaiKhoan()
        {
            InitializeComponent();
            dgvDSTaiKhoan.AutoGenerateColumns = false;
        }

        private void frmQLTaiKhoan_Load(object sender, EventArgs e)
        {
            _repository = new GeneralRepository<TaiKhoan>();
            _manager = new TaiKhoanManager(_repository);
            dgvDSTaiKhoan.DataSource = _repository.GetAll().ToList();
            txtUserName.Focus();
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dgvDSTaiKhoan.DataSource = _repository.GetAll().ToList();
            txtUserName.ResetText();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new frmAddTaiKhoan(_manager);
            frm.Text = "Thêm tài khoản";
            frm.ShowDialog();
            dgvDSTaiKhoan.DataSource = _repository.GetAll().ToList();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa các tài khoản đã chọn?", "Xóa tài khoản", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            foreach (DataGridViewRow row in dgvDSTaiKhoan.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
                    _manager.Delete(row.Cells[1].Value.ToString());
            }
            dgvDSTaiKhoan.DataSource = _repository.GetAll().ToList();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvDSTaiKhoan.SelectedRows.Count > 0)
            {
                _tk = new TaiKhoan();
                DataGridViewRow row = dgvDSTaiKhoan.SelectedRows[0];
                _tk.Username = row.Cells[1].Value.ToString();
                _tk.Password = row.Cells[2].Value.ToString();
                _tk.PhanQuyen = row.Cells[3].Value.ToString();

                var frm = new frmAddTaiKhoan(_manager, _tk);
                frm.Text = "Cập nhật tài khoản";
                frm.ShowDialog();
                dgvDSTaiKhoan.DataSource = _repository.GetAll().ToList();
            }
            else
                MessageBox.Show("Chưa chọn giáo viên để sửa", "Thông báo");
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.Username.IndexOf(txtUserName.Text) >= 0);
            if (aResult.Success)
                dgvDSTaiKhoan.DataSource = aResult.Result;
            else
            {
                MessageBox.Show(aResult.GetErrorMessages());
                btnLamMoi.PerformClick();
                txtUserName.ResetText();
                txtUserName.Focus();
            }
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.Username.IndexOf(txtUserName.Text) >= 0);
            if (aResult.Success)
                dgvDSTaiKhoan.DataSource = aResult.Result;
            else
                dgvDSTaiKhoan.DataSource = null;
        }

        private void dgvDSTaiKhoan_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSTaiKhoan.Rows.Count > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < dgvDSTaiKhoan.Rows.Count; idx++)
                {
                    dgvDSTaiKhoan.Rows[idx].Selected = _selectAll;
                    dgvDSTaiKhoan.Rows[idx].Cells[0].Value = _selectAll;
                }
            }
        }

        private void dgvDSTaiKhoan_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSTaiKhoan.Rows[e.RowIndex].Cells[0].Value != null)
            {
                bool selected = (bool)dgvDSTaiKhoan.Rows[e.RowIndex].Cells[0].Value;
                dgvDSTaiKhoan.Rows[e.RowIndex].Selected = selected;
            }
        }
    }
}
